package com.jwell.panprovider.ui.adapter

import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.panprovider.R
import com.jwell.panprovider.entity.TransportPlaneBean
import com.jwell.panprovider.tools.StringUtils
import com.jwell.panprovider.ui.activity.TransportPlanInfoActivity
import kotlinx.android.synthetic.main.item_transport_plane_layout.view.*


/**
 * Created by 76577 on 2018/4/21.
 * 运输计划列表适配器
 */
class TransportPlaneAdapter constructor(list: List<TransportPlaneBean>) : RecyclerView.Adapter<TransportPlaneAdapter.ViewHolder>() {


    private var list = list

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_transport_plane_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        with(holder.itemView) {
            when (list[position].fuelState) {
                1 -> {
                    text_state.text = "待供应"
                    text_provide_carNum.text = list[position].canGrabCount.toString() + "车"
                    text_provide_carNum.visibility = View.VISIBLE
                    text_provide_info.text = "剩余可供应"
                    if (list[position].isUnlimited) {
                        text_provide_carNum.text = "不限"
                    }
                }
                2 -> {
                    text_state.text = "供应中"
                    text_provide_carNum.text = list[position].canGrabCount.toString() + "车"
                    text_provide_carNum.visibility = View.VISIBLE
                    text_provide_info.text = "剩余可供应"
                    if (list[position].isUnlimited) {
                        text_provide_carNum.text = "不限"
                    }
                }
                3 -> {
                    text_state.text = "供应结束"
                    text_provide_carNum.visibility = View.GONE
                    text_provide_info.text = "供应结束"
                }
                else -> {
                    text_state.text = ""
                    text_state.text = "供应结束"
                    text_provide_carNum.visibility = View.GONE
                    text_provide_info.text = "供应结束"
                }
            }
            text_code.text = list[position].planCode ?: ""
            text_provide_time.text = StringUtils.getInstance()
                    .setModal(StringUtils.TIME_TYPE_1)
                    .regularization(list[position].planDate, "T", " ")
            text_receiptCom.text = list[position].receivingUnit ?: ""
            text_goodsName.text = list[position].materialName ?: ""
            //累计供应
            val carNum = list[position].supplyCount
            val weight = list[position].supplyTon
            text_provide_carNum_weight.text = carNum.toString() + "车/" + weight + "吨"
            if (list[position].supplyCount >= 0) {
                text_provider_num.text = "已供应" + list[position].supplyCount + "车"
            } else {
                text_provider_num.text = ""
            }

            holder.itemView.setOnClickListener {
                var intent = Intent(holder.itemView.context, TransportPlanInfoActivity::class.java)
                intent.putExtra("id", list[position].id)
                holder.itemView.context.startActivity(intent)
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}